/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webservice.notafiscalpaulista;

import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.net.URL;
import java.util.TimerTask;
import javax.swing.JOptionPane;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 *
 * @author klinger
 */
public class WsConsulta extends TimerTask {

    String usuario;
    String senha;
    String caminhoArquivo;
    String cnpj;
    String categoria;

    public WsConsulta(String usuario, String senha, String caminhoArquivo, String cnpj, String categoria) {
        this.usuario = usuario;
        this.senha = senha;
        this.caminhoArquivo = caminhoArquivo;
        this.cnpj = cnpj;
        this.categoria = categoria;
    }

    @Override
    public void run() {
        wsConsulta(usuario, senha, caminhoArquivo, cnpj, categoria);
    }

    public static String wsConsulta(String usuario, String senha, String caminhoArquivo, String cnpj, String categoria) {

        String retConsSitNFe = null;

        try {

            StringBuilder soapMessage = new StringBuilder();

            String urlAdders = "https://www.nfp.fazenda.sp.gov.br/ws/arquivonf_mod1.asmx";

            soapMessage.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
                    .append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"")
                    .append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"")
                    .append(" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">")
                    .append("<soap12:Header>")
                    .append("<Autenticacao Usuario=\"" + usuario + "\" Senha=\"" + senha + "\" CNPJ=\"" + cnpj + "\" CategoriaUsuario=\"" + categoria + "\" xmlns=\"https://www.nfp.sp.gov.br/ws\" />")
                    .append("</soap12:Header>")
                    .append("<soap12:Body>")
                    .append("<Enviar xmlns=\"https://www.nfp.sp.gov.br/ws\">")
                    .append("<NomeArquivo>" + caminhoArquivo + "</NomeArquivo>")
                    .append("<ConteudoArquivo></ConteudoArquivo>")
                    .append("<EnvioNormal>true</EnvioNormal>")
                    .append("<Observacoes></Observacoes>")
                    .append("</Enviar>")
                    .append("</soap12:Body>")
                    .append("</soap12:Envelope>");

            MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

            MimeHeaders header = new MimeHeaders();
            header.addHeader("Content-Type", "application/soap+xml");

            SOAPMessage message = factory.createMessage(header, new ByteArrayInputStream(soapMessage.toString().getBytes()));

            SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection();
            URL url = new URL(urlAdders);

            SOAPMessage res = con.call(message, url);
            con.close();

            StringWriter sw = new StringWriter();
            Source source = new DOMSource(res.getSOAPPart().getEnvelope().getBody().getFirstChild());
            TransformerFactory.newInstance().newTransformer().transform(source, new StreamResult(sw));
            retConsSitNFe = sw.toString();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage().toString());
        }

        return retConsSitNFe;
    }
}
